<?php 
/*
 * News 
 * (c) Taras Kudla, 2010
 */
class mod_news extends module{
	# news storing table
	private $table = "mod_news";
	# news categories
	//private $table_categories = "mod_news_categories";	
	# news resources
	private $table_resources = "mod_news_resources";
	# rss feeds
	private $table_rssfeeds = "mod_news_rssfeeds";	
	# records per page
	private $maxrecords = 10;
	# default categoryid
	private $defaultcatid=0;
	# maxsize of image
	private $imagex = 250;
	private $imagey = 0;
	# maxsize of small image
	private $imagesmallx = 88;
	private $imagesmally = 0;
	# current item
	private $currentitem = 0;
	
	public function __call($method,$params){
		$method=$this->api->escapeString($method);
		# checking for category
		$nid = false;
		//$cid = $this->api->dbo->select($this->table_categories,"cat_id","where cat_alias='$method' limit 1 offset 0",_RESULT_SINGLE);
		//if( $cid==null){
		//	return null;
		//}
		# 
		# checking for news or category loading
		if( !$nid){
			# category loading
			//return $this->catload($cid);
		} else {
			return $this->itemload($nid);
		}
	}
	public function page(){
		$p = 0;
		if( isset($_GET["id"]) && is_numeric($_GET["id"])){
			$p = (int)$_GET["id"];
		}
		
		return $this->defaultaction($p);
	}
	
	public function defaultaction($page=0){
		if( isset($_GET["key"])){
			return $this->itemload($_GET["key"]);
		}		
		# getting total records
		$total = $this->api->dbo->select($this->table,"COUNT(*) as count",null,_RESULT_SINGLE);
		# pages management
		$pages=(int)round($total/$this->maxrecords+0.459);
		if( $page<0 || $page>$pages)$page=0;
        //$page=()  ( isset($_GET["page"]) && is_numeric($_GET["page"]) && $_GET["page"]<=$pages && $_GET["page"]>0 )?$_GET["page"]:0;
        $sqllimit="limit ".$this->maxrecords." offset ".($page*$this->maxrecords);
		# getting latest news
		$data = $this->api->dbo->select($this->table,
			"files.name as imagename,".$this->table.".*",
			"left join files on ".$this->table.".imagesmall=files.id
			 order by id desc $sqllimit");
		# pages text
		$pagestext=$this->api->tools->pages("page",$pages,$page,"id");
		return $this->api->view->template("startlist",array("data"=>$data,"pages"=>$pagestext,"imagesurl"=>$this->api->parseasurl($this->api->get("uploadsurl"))));
	}
	public function lastadded(){
		$data = $this->api->dbo->select($this->table,"id,title","order by created desc limit 5 offset 0");
		return $this->api->view->template("last",array("data"=>&$data));
	}	
	/* Frontend */
	public function menucategories(){
		/*$data = $this->api->dbo->select($this->table_categories,"cat_name,cat_alias","where cat_id!=0");
		return $this->api->view->template("menucategories",array("data"=>$data));*/
	}
	
	public function relatedblock(){
		$id = $this->currentitem;
		if( $id == 0){
			return null;
		}
		$title = $this->api->dbo->select($this->table,"title","where id='$id'",_RESULT_SINGLE);
		if( $title==null){
			return null;
		}
		$data = $this->api->dbo->select($this->table,"id, title, MATCH (title) AGAINST
			('$title') AS score ","WHERE MATCH (title) AGAINST
			('$title') and id!='$id' limit 5 offset 0");
		if( count($data)==0) return null;
		return $this->api->view->template("rel",array("data"=>&$data));
	}
	
	private function catload($id){
		/*$this->api->base->addway("defaultaction",null,$this->lang->get("n1"));
		# getting total records
		$total = $this->api->dbo->select($this->table,"COUNT(*) as count","where cat_id='$id'",_RESULT_SINGLE);
		# pages management
		$pages=(int)round($total/$this->maxrecords+0.459);	
        $page=( isset($_GET["page"]) && is_numeric($_GET["page"]) && $_GET["page"]<=$pages && $_GET["page"]>0 )?$_GET["page"]:0;
        $sqllimit="limit ".$this->maxrecords." offset ".($page*$this->maxrecords);
        # getting data
		$data = $this->api->dbo->select($this->table,
			"files.name as imagename,".$this->table.".*",
			"left join files on ".$this->table.".imagesmall=files.id
			 where {$this->table}.cat_id='$id' order by id desc $sqllimit");
        # category
        $category = $this->api->dbo->select($this->table_categories,"*","where cat_id='$id'",_RESULT_ROW);
		$this->api->base->addway($category["cat_alias"],null,$category["cat_name"]);
        # pages text
		$pagestext=$this->api->tools->pages($category["cat_alias"],$pages,$page);
		return $this->api->view->template("categorydisplay",array("data"=>$data,"category"=>$category,"pagestext"=>$pagestext,"imagesurl"=>$this->api->parseasurl($this->api->get("uploadsurl"))));
		*/
	}
	
	private function itemload($id){
		//$this->api->base->addway("defaultaction",null,$this->lang->get("n1"));
		$id=(int)$id;
		$data = $this->api->dbo->select($this->table,
			"files.name as imagename,".$this->table.".*",
			"left join files on ".$this->table.".imagesmall=files.id
			 where {$this->table}.id='$id'",_RESULT_ROW);		
		if( count($data)==0)
			return null;
		$this->currentitem = $id;
		$this->api->base->addway("defaultaction",$data["id"],$data["title"]);
		return $this->api->view->template("viewitem",array("data"=>&$data,"imagesurl"=>$this->api->parseasurl($this->api->get("uploadsurl"))));
	}
	
	
	/************* Administration ************************************/
	public function newarticle(){
		# checking if post data entered
		if( $this->api->checkPOST("title","head","body","language")){			
			list($title,$language)=$this->api->escapePOST("title","language");
			$head = $this->api->escapeString($_POST["head"],array(">","<"));
			if( empty($title) || empty($head)){
				print $this->api->message($this->lang->get("n15")); 
			} else {
			$body = $this->api->escapeString($_POST["body"],array(">","<"));
			if( $language == "ru"){
				$alias = $this->api->tools->rustotranslit($title);
			}
			else {
				$alias = $this->api->tools->ukrtotranslit($title);
			}
			# checking for image
			$image = 0;
			$imagesmall = 0;
			$imagesuccess = true;
			if( !empty($_FILES["image"]["name"])){
				$this->api->setstate("exceptiononerror",true);
				try {
					$image = $this->api->file->copyuploadedfile("image",array("jpg","jpeg","png","gif"));
					$size = $this->api->file->imagesize($image);
					$width = $size[0];
					$height = $size[1];
					if( $width > $this->imagex)
						$image = $this->api->file->resizeimage($image,$this->imagex);
					$imagesmall = $this->api->file->copysysfile($image);
					$imagesmall = $this->api->file->resizeimage($imagesmall,$this->imagesmallx);
				} catch ( GepardException $e){
					$imagesuccess=false;
					$this->api->file->delete($image);
					$this->api->file->delete($imagesmall);
					$this->api->restorestate("exceptiononerror");
				}
				$this->api->restorestate("exceptiononerror");
			}
			if( $this->api->dbo->select($this->table,"id","where alias='$alias'",_RESULT_BOOL)){
				print $this->api->message($this->lang->get("n14"));
			} else if(!$imagesuccess){
				print $this->api->message($this->lang->get("n26"));
			} else {				
				# adding new record to table
				$this->api->dbo->insert($this->table,array("title"=>$title,"alias"=>$alias,"language"=>$language,"authorname"=>$this->api->user->GetUserName(),
				"head"=>$head,"body"=>$body,"resource_id"=>0,"created"=>date("Y-m-d H:i:s"),"rating"=>0,
				"image"=>$image,"imagesmall"=>$imagesmall,"sourcelink"=>""));
				print $this->api->message($this->lang->get("n13"));
			}
			}
		}
		# calling for tinymce environment
		$tinymce = $this->api->libload("tinymce");
		$tinymce->set(_TINYMCE_FULL);	
		# building template
		return $this->api->view->template("newarticle",array("lang"=>$this->api->syslanguages()));		
	}
	
	public function editarticle(){
		
	}
	
	public function viewarticles(){
		# getting total records
		$total = $this->api->dbo->select($this->table,"COUNT(*) as count",null,_RESULT_SINGLE);
		# pages management
		$pages=(int)round($total/$this->maxrecords+0.459);	
        $page=( isset($_GET["page"]) && is_numeric($_GET["page"]) && $_GET["page"]<=$pages && $_GET["page"]>0 )?$_GET["page"]:0;
        $sqllimit="limit ".$this->maxrecords." offset ".($page*$this->maxrecords);
		# getting information
		$data = $this->api->dbo->select($this->table,
			"files.name as imagename,".$this->table.".*",
			"left join files on ".$this->table.".imagesmall=files.id
			 order by id desc $sqllimit");
		if( count($data)==0){
			return $this->api->message($this->lang->get("n20"));			
		}
		# pages text
		$pagestext=$this->api->tools->pages("viewarticles",$pages,$page);
		return $this->api->view->template("list",array("data"=>$data,"pages"=>$pagestext,"imagesurl"=>$this->api->parseasurl($this->api->get("uploadsurl"))));
	}
	public function viewarticle(){
		# adding global way
		$this->api->base->addway("viewarticles",null,$this->lang->get("n1"));
		if( !isset($_GET["id"]) || !is_numeric($_GET["id"])){
			return $this->api->message($this->lang->get("n16"));
		}
		$id = (int)$_GET["id"];
		$data = $this->api->dbo->select($this->table,
			"*","where id='$id' limit 1 offset 0",_RESULT_ROW);
		if( count($data)==0){
			return $this->api->message($this->lang->get("n17"));
		}
		# image
		$image = false;
		if( (int)$data["image"]!=0){
			$image = true;
			$data["image"]= $this->api->file->getfileurl((int)$data["image"]); 
		}
		# adding global way
		$this->api->base->addway("viewarticle","id=".$id,$this->lang->get("n11"));
		# printing template
		return $this->api->view->template("viewarticle",array("data"=>$data,"image"=>$image));
	}
	public function deletearticle(){
		# adding global way
		$this->api->base->addway("viewarticles",null,$this->lang->get("n1"));
		if( !isset($_GET["id"]) || !is_numeric($_GET["id"])){
			return $this->api->message($this->lang->get("n16"));
		}
		$id = (int)$_GET["id"];		
		$link = $this->api->base->buildactionlink("viewarticles",null,$this->lang->get("n18"));
		$this->api->dbo->delete($this->table,"where id='$id'");
		print $this->api->message($this->lang->get("n19")."&nbsp;".$link);
	}
	/*
	 * Categories management
	 */
	public function categories(){
		/*# checking for deleting
		if( isset($_GET["delete"]) && is_numeric($_GET["delete"])){
			$id = (int)$_GET["delete"];
			if( $id!=0){
				$this->api->dbo->delete($this->table,"where cat_id='$id'");
				$alias = $this->api->dbo->select($this->table_categories,"cat_alias","where cat_id='$id' limit 1 offset 0",_RESULT_SINGLE);
				if( $alias!=null){
					$this->api->tools->deleteaction("news",$alias);
					$this->api->dbo->delete($this->table_categories,"where cat_id='$id'");
					$this->api->dbo->delete($this->table_rssfeeds,"where cat_id='$id'");
				}
			}
		}
		# checking for adding
		if( $this->api->checkPOST("name","description","Submit")){
			list($name,$description,$alias)=$this->api->escapePOST("name","description","alias");
			if( empty($alias)){
				$language = $this->api->get("language");
				if( $language=="ua"){
					$latinname = $this->api->tools->ukrtotranslit($name);
				} else
					$latinname = $this->api->tools->rustotranslit($name);
				# preparing alias name
				$alias = $this->api->tools->preparealiasname($latinname);
			} else {
				$alias = $this->api->tools->ukrtotranslit($alias);
				$alias = $this->api->tools->preparealiasname($alias);
			}
			if( $this->api->dbo->select($this->table_categories,"cat_id","where cat_name='$name' limit 1 offset 0",_RESULT_BOOL)){
				print $this->api->message($this->lang->get("c8"));
			} else if($this->api->dbo->select($this->table_categories,"cat_id","where cat_alias='$alias' limit 1 offset 0",_RESULT_BOOL)){
				print $this->api->message($this->lang->get("c9"));
			} else {
				# everything is ok, adding to database
				$this->api->dbo->insert($this->table_categories,array("cat_name"=>$name,"cat_alias"=>$alias,"cat_description"=>$description,"created"=>date("Y-m-d H:i:s")));
				# creating new action for category
				$this->api->tools->createaction("news",$alias,true);
				print $this->api->message($this->lang->get("c10"));
			}
		}
		$data = $this->api->dbo->select($this->table_categories,"cat_id,cat_name,cat_alias","where cat_alias!='default'");			
		return $this->api->view->template("categories",array("data"=>$data));
		*/
	}
	/*
	 * News resources managing
	 */
	public function resources(){
		
	}
	
	public function newresource(){

	}
	
	/*
	 * RSS feeds managing
	 */
	public function rssfeeds(){
		# checking for deleting
		if( isset($_GET["delete"]) && is_numeric($_GET["delete"])){
			$feedid = (int)$_GET["delete"];
			$this->api->dbo->delete($this->table_rssfeeds,"where feed_id='$feedid'");	
		}
		# checking for input data
		if( $this->api->checkPOST("link","resource","name")){
			list($resource,$name)=$this->api->escapePOST("resource","name");
			$resource = (int)$resource;
			$link = $this->api->escapeString($_POST["link"]);
			$rss = $this->api->libload("rss");
			$rssdata = $rss->load($link);
			if( !$rssdata){
				print $this->api->message($this->lang->get("r4"));
			} else if( !$rss->validate($rssdata)){
				print $this->api->message($this->lang->get("r5"));
			} else {
				# everything is ok
				$data = $rss->parse($rssdata);
				unset($rssdata);
				if( empty($name)){
					$name = $this->api->escapeString($rssdata["title"]);
				}
				# inserting
				$this->api->dbo->insert($this->table_rssfeeds,array("feed_name"=>$name,"feed_url"=>$link,
				"resource_id"=>$resource,"created"=>date("Y-m-d H:i:s"),"denytotal"=>0));
				print $this->api->message($this->lang->get("r6"));				
			}			
		}	
		$res = $this->api->dbo->select($this->table_resources,"resource_id,resource_name");
		$data = $this->api->dbo->select(array($this->table_rssfeeds=>"f",
			$this->table_resources=>"r"),"f.feed_id,f.feed_name,f.feed_url,r.resource_name",
			"where r.resource_id=f.resource_id");
		return $this->api->view->template("rssfeeds",array("data"=>$data,"res"=>$res));
	}
	
	public function parseallrss(){
		$data = $this->api->dbo->select($this->table_rssfeeds);
		$rss = $this->api->libload("rss");
		foreach($data as $k=>$channel){
			$link = $channel["feed_url"];
			$rssdata=$rss->load($link);
			if( !$link){
				$this->api->dbo->update($this->table_rssfeeds,"set denytotal=denytotal+1 where feed_id='".$channel["feed_id"]."' ");
				continue;
			}
			$rssdata = $rss->parse($rssdata);
			foreach ( $rssdata["item"] as $i=>$v){
				$name = $this->api->escapeString($v["title"]);
				if( $this->api->dbo->select($this->table,"id","where title='$name'",_RESULT_BOOL)){
					continue;
				}
				$this->api->dbo->insert($this->table,array("title"=>$name,"head"=>$this->api->escapeString($v["description"],array(">","<","\"")),
					"body"=>$this->api->escapeString($v["fulltext"],array(">","<","\"")),"authorname"=>$channel["feed_name"],"created"=>date("Y-m-d H:i:s"),
					"language"=>$this->api->get("language"),"alias"=>"",
					"resource_id"=>0,"rating"=>0));
			}
		}
	}
	public function finansistuaparser(){
		$starttime = microtime(1);
		$total = 0;
		$parsed = 0;
		# finance.ua
		$f = array("http://news.finance.ua/ru/~/1/80","http://news.finance.ua/ru/~/1/120","http://news.finance.ua/ru/~/1/110","http://news.finance.ua/ru/~/1/70","http://news.finance.ua/ru/~/1/30","http://news.finance.ua/ru/~/1/20");
		$i=0;
		$this->api->setstate("exceptiononerror",true);
		foreach( $f as $v1=>$v){
			try {
			$data = @file_get_contents($v);
			if( !$data ) 
				continue;
			$data = strip_tags($data,"<a>");
			$data = preg_match_all("/<a\s*[^>]+href=(\"|')(\/ru\/\~\/1\/[0-9]{1,2}\/all\/[0-9]{4}\/[0-9]{2}\/[0-9]{2}\/[^\\1]+)\\1[^>]*\>(.+)<\/a\s*>/Uis",$data,$tmp);
			$data = null;
			$links = $tmp[2];
			$titles = $tmp[3];
			foreach( $links as $k=>$link){
				$link = "http://news.finance.ua".$link;
				$data = file_get_contents($link);
				if( preg_match("/<div\s*[^>]+class=\"news-body\">(.+)<\/div>/Uis",$data,$tmp) ){
					$text = mb_convert_encoding($tmp[1],"utf-8","windows-1251");
					$title = mb_convert_encoding($titles[$k],"utf-8","windows-1251");
					print "finance.ua - $i ($link) - item parsed ";
					if( $this->smartadd($title,$text,
					"<noindex><a href=\"http://finance.ua\" rel=\"nofollow\">finance.ua</a></noindex>"))
					{ print "+"; $total++; } else print "-";
					print "\n";
				} else { print "Cannot parse finance.ua, format changed\n"; continue; }
				$i++;
				$parsed++;
			}
			} catch ( GepardException $e){
				print "Error - ".$e->getMessage()." \n";
			}			
		}
		
		# bin.ua
		try {
		$data = file_get_contents("http://bin.ua/news");
		$links = preg_match_all("/\<\s*span\s*class=\"short_story_title\"\s*\>\s*<a\s*href=\"(\/news\/[^\"]+)\"\s*>(.+)<\/a>\<\/span\>/Uis",$data,$tmp);
		$links = $tmp[1];
		$titles = $tmp[2];
		$i=0;
		foreach($links as $k=>$link){
			$link = "http://bin.ua".$link;
			$data = @file_get_contents($link);
			if( !$data) {
				print "Cannot load bin.ua\n";
				continue;
			}
			$data = mb_convert_encoding($data,"utf-8","windows-1251");	
			if( preg_match("/\<div\s*id=\'news-id-[0-9]+\'\s*\>(.+)\<\/div\>/Uis",$data,$tmp) ){
				$text = str_replace("&shy;","",$tmp[1]);
				$text = strip_tags($text,"<p><br><span>");
				$source = preg_match("/По\s*материалам\s*:(.+)<\/div>/Uis",$data,$tmp) ? $tmp[1]:"<noindex><a href=\"http://bin.ua\" rel=\"nofollow\">bin.ua</a></noindex>";  
				$title = mb_convert_encoding($titles[$k],"utf-8","windows-1251");
				$title = str_replace("&shy;","",$title);
				print "bin.ua - $i ($link) - item parsed ";
				if( $this->smartadd($title,$text,$source))
				{ print "+"; $total++; } else print "-";
				print "\n";
			} else {
				# формат не совпадает
				print "Cannot parse bin.ua item, format changed\n";
				continue;
			}
			$i++;
			$parsed++;
		}
		}  catch ( GepardException $e){
				print "Error - ".$e->getMessage()." \n";
		}	
		# fundmarket.ua
		try {
		$data = file_get_contents("http://fundmarket.ua/news");
		$links = preg_match_all("/<a\s*href=(\/news\/[^\/]+\/)\s*class=\"ilink\">(.+)<\/a>/Uis",$data,$tmp);
		$links = $tmp[1];
		$titles = $tmp[2];
		$i=0;
		foreach( $links as $k=>$link){
			$link = "http://fundmarket.ua".$link;
			$data = @file_get_contents($link);
			if( !$data ) {
				continue;
			}
			$data = mb_convert_encoding($data,"utf-8","windows-1251");
			$pos1 = strpos($data,"class=\"text\"");
			if( $pos1 === false ) {
				continue;
			}
			$data = substr($data,$pos1+14,(strlen($data)-$pos1-14));
			$pos2 = strpos($data,"class=\"date3\"",10);
			if( $pos2 === false ) {
				continue;
			}
			$data = substr($data,0,$pos2-5);
			$pos3 = strpos($data,"</div>");
			if( $pos3 === false ) { 
				continue;
			}
			$data = substr($data,$pos3+6,strlen($data)-$pos3-4);
			$data = trim(strip_tags($data,"<p><br>"));
			$title = strip_tags(mb_convert_encoding($titles[$k],"utf-8","windows-1251"));
			//$data = preg_replace("/\s+/"," ",$data);
			$source = preg_match("/Источник\s*:(.+)<\/div>/Uis",$data,$tmp) ? $tmp[1]:"<noindex><a href=\"http://fundmarket.ua\" rel=\"nofollow\">fundmarket.ua</a></noindex>"; 
			print "fundmarket.ua - $i ($link) - item parsed ";
			if( $this->smartadd($title,$data,$source) )
			{ print "+"; $total++; } else print "-";
			print "\n";
			$i++;
			$parsed++;
		}
		}  catch ( GepardException $e){
				print "Error - ".$e->getMessage()." \n";
		}	
		# investfunds.com.ua
		try {
		$link = "http://investfunds.com.ua/news/";
		$data = file_get_contents($link);
		preg_match_all("/<a\s*href='(news\/[^']+)'\s*class=u\s*>(.+)<\/a>/Uis",$data,$tmp);		
		$links = $tmp[1];
		$titles = $tmp[2];
		$i=0;
		foreach( $links as $k=>$link){
			$link = "http://investfunds.com.ua/".$link;
			$data = @file_get_contents($link);
			if( !$data ) {
				continue;
			}
			if( !preg_match("/<font\s*size=2\s*>(.+)<\/font>/Uis",$data,$tmp) ) {
				continue;
			}
			$title =strip_tags(mb_convert_encoding($titles[$k],"utf-8","windows-1251"));
			$text = strip_tags(mb_convert_encoding($tmp[1],"utf-8","windows-1251"),"<p><br>");
			print "investfunds.com.ua - $i ($link) - item parsed ";
			if( $this->smartadd($title,$text,"<noindex><a href=\"http://investfunds.com.ua\" rel=\"nofollow\">investfunds.com.ua</a></noindex>"))
			{ print "+"; $total++; } else print "-";
			print "\n";
			$i++;
			$parsed++;
		}
		}  catch ( GepardException $e){
				print "Error - ".$e->getMessage()." \n";
		}
		# ugmk.com.ua - не парситься ((
		$this->api->restorestate("exceptiononerror");
		print date("Y-m-d").", time: ".round(microtime(1)-$starttime,4).", parsed - $parsed, added - $total\n";
		# optimizing sql table
		$this->api->dbo->execute("optimize table {$this->table}");
		return true;
	}
	
	# все данные должны быть предварительно конвертированы в utf-8 
	private function smartadd($title,$text,$source=""){
		$title = trim($this->api->escapeString(strip_tags($title)));
		$text = $this->api->escapeString($text,array(">","<","\""));
		$source = $this->api->escapeString($source,array("\"","<",">"));
		$id=$this->api->dbo->select($this->table,"id","where title='$title' limit 1 offset 0",_RESULT_SINGLE);
		if( $id!=null ){
			print " Skipped:($id) exists ";
			return false;
		} else{
			$head = $text;
			$body = "";
			if( strlen($text)>300){
				for($i=300;$i<strlen($text);$i++){
					if( $text[$i]==" " || $text[$i]=="." || $text[$i]=="?" || $text[$i]=="!"){
						$head = substr($text,0,$i);
						$body = substr($text,$i,strlen($text)-$i);
						break;	
					}
				}
			}
			if( strlen($title)>240){ 
				print " Skipped: title is too long ";
				return false;
			}
			if( preg_match("/interfax/i",$text) || preg_match("/rbc/i",$text) || preg_match("/Интерфакс/",$text)
				|| preg_match("/УНИАН/",$text) || preg_match("/unian/i",$text) ){
					print " Skipped: property words found ";
					return false;
			}
			$this->api->dbo->insert($this->table,array("title"=>$title,"alias"=>"","language"=>"ru","authorname"=>$source,
				"head"=>$head,"body"=>$body,"resource_id"=>0,"created"=>date("Y-m-d H:i:s"),"rating"=>0,
				"image"=>0,"imagesmall"=>0,"sourcelink"=>""));
			return true;
		}
		return false;		
	}
	
	
	
	/*
	 * Ajax tools
	 */
	public function ajaxtools(){
		
	}
	/*
	 * Options
	 */
	public function options(){
		
	}
	
	public function install(){
		# items
		$table = new sqltable($this->table);
		$table->addfield("id",_FIELD_INTAUTO);
		$table->addfield("title",_FIELD_CHAR);
		//$table->addfield("cat_id",_FIELD_INT);
		$table->addfield("language",_FIELD_CHAR);
		$table->addfield("alias",_FIELD_CHAR);
		$table->addfield("image",_FIELD_INT,11,0);
		$table->addfield("imagesmall",_FIELD_INT,11,0);
		$table->addfield("head",_FIELD_TEXT);
		$table->addfield("body",_FIELD_TEXT);
		$table->addfield("authorname",_FIELD_CHAR);
		$table->addfield("sourcelink",_FIELD_CHAR);
		$table->addfield("created",_FIELD_DATETIME);
		$table->addfield("resource_id",_FIELD_INT);
		$table->addfield("rating",_FIELD_INT,10,0);
		$table->addkey(_KEY_FULLTEXT,"title");
		$this->api->dbo->createtable($table);
		unset($table);
		# categories
		//$table = new sqltable($this->table_categories);
		//$table->addfield("cat_id",_FIELD_INTAUTO);
		//$table->addfield("cat_name",_FIELD_CHAR);
		//$table->addfield("cat_alias",_FIELD_CHAR);
		//$table->addfield("cat_description",_FIELD_TEXT);
		//$table->addfield("created",_FIELD_DATETIME);
		//$this->api->dbo->createtable($table);
		//unset($table);
		# resources		
		$table = new sqltable($this->table_resources);
		$table->addfield("resource_id",_FIELD_INTAUTO);
		$table->addfield("resource_name",_FIELD_CHAR);
		$table->addfield("resource_link",_FIELD_CHAR);
		$table->addfield("enabled",_FIELD_INT,1,1);
		$this->api->dbo->createtable($table);
		unset($table);
		# rss feeds
		$table = new sqltable($this->table_rssfeeds);
		$table->addfield("feed_id",_FIELD_INTAUTO);
		$table->addfield("feed_name",_FIELD_CHAR);
		$table->addfield("feed_url",_FIELD_CHAR);
		//$table->addfield("cat_id",_FIELD_INT);
		$table->addfield("resource_id",_FIELD_INT);
		$table->addfield("created",_FIELD_DATETIME);
		$table->addfield("denytotal",_FIELD_INT);
		$this->api->dbo->createtable($table);
		# adding null category
		//$this->api->dbo->insert($this->table_categories,array("cat_name"=>"default","cat_alias"=>"default","cat_description"=>"","created"=>date("Y-m-d H:i:s")));
		//$this->api->tools->createaction("news","default",true);
		//$this->api->dbo->update($this->table_categories,"set cat_id='".$this->defaultcatid."' where cat_alias='default'");
		# adding local resource
		$this->api->dbo->insert($this->table_resources,array("resource_name"=>"local","resource_link"=>""));		
	}
	public function uninstall(){
		$this->api->dbo->droptable($this->table);
		//$this->api->dbo->droptable($this->table_categories);
		$this->api->dbo->droptable($this->table_resources);
	}	
}
?>
